package com.xtaller.sdboot.core.base;

import com.alibaba.fastjson.annotation.JSONField;
import com.xtaller.sdboot.utils.helper.JsonHelper;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;

/**
 * @author : Taller
 * @date : 2020/1/14 23:58
 * @Description :
 */
@Data
public abstract class BaseEntity implements Serializable {
    /***
     * 默认主键字段id
     */
    @TableId(type = IdType.UUID)
    private String id;

    /***
     * 默认逻辑删除标记，isDeleted=0有效
     */
    @TableLogic
    @JSONField(serialize = false)
    @TableField("isDeleted")
    private boolean deleted = false;

    /***
     * 创建人
     */
    @TableField("createBy")
    private String createBy;

    /***
     * 新建时由数据库赋值
     */
    @TableField(value = "createTime", insertStrategy = FieldStrategy.NEVER, updateStrategy = FieldStrategy.NEVER)
    @JSONField(format="yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    /***
     * 更新时由数据库赋值
     */
    @TableField(value = "updateTime", insertStrategy = FieldStrategy.NEVER, updateStrategy = FieldStrategy.NOT_NULL)
    @JSONField(format="yyyy-MM-dd HH:mm:ss")
    private Date updateTime;


    /***
     * Entity对象转为map
     * @return
     */
    public Map<String, Object> toMap(){
        String jsonStr = JsonHelper.stringify(this);
        return JsonHelper.toMap(jsonStr);
    }

    /**
     * Entity对象转为String
     * @return
     */
    @Override
    public String toString(){
        return this.getClass().getName()+ ":"+this.getId();
    }

}
